package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.goddess.yeecolor.Modle.CurriculumModle;
import com.goddess.yeecolor.R;

import java.util.List;

import static com.goddess.yeecolor.R.color.jiageColor;
import static com.goddess.yeecolor.R.color.red;
import static com.goddess.yeecolor.R.color.textColor_noback;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/13
 * 作用: ******
 * 备注: 课程表Adapter
 */
public class CurriculumFragmentViewPagerAdapter extends RecyclerView.Adapter<CurriculumFragmentViewPagerAdapter.CurriculumViewHolder> {
    private List<CurriculumModle> modles;
    private Context context;

    /**
     * 构造
     *
     * @param modles  实体类集合
     * @param context 上下文
     */
    public CurriculumFragmentViewPagerAdapter(List<CurriculumModle> modles, Context context) {
        this.modles = modles;
        this.context = context;
    }



    @Override
    public CurriculumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.curriculum_viewpager_item_layout, parent, false);
        return new CurriculumViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CurriculumViewHolder holder, int position) {
        CurriculumModle modle = modles.get(position);
        holder.tv_title.setText(modle.getTitle());
        holder.tv_name.setText(modle.getName());
        holder.tv_word.setText(modle.getWord());
        holder.tv_date.setText(modle.getDate());
        holder.webview =modle.getWebview();
        holder.id =modle.getId();
        holder.price =modle.getPrice();

        int state =modle.getState();
        if(state==0){
            holder.btn_state.setBackgroundResource(R.drawable.exercisefrg_btn);
            holder.btn_state.setText("预告");
            holder.btn_state.setTextColor(context.getResources().getColor(jiageColor));
        }else if(state==1){
            holder.btn_state.setBackgroundResource(R.drawable.exercisefrg_btn1);
            holder.btn_state.setText("正在直播");
            holder.btn_state.setTextColor(context.getResources().getColor(red));
        }else if(state==2){
            holder.btn_state.setBackgroundResource(R.drawable.exercisefrg_btn3);
            holder.btn_state.setText("直播结束");
            holder.btn_state.setTextColor(context.getResources().getColor(textColor_noback));
        }


    }

    @Override
    public int getItemCount() {
        return  modles.size();
    }
    class CurriculumViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_title, tv_date, tv_name, tv_word ;
        private Button btn_state;
        private String webview, id,price;
        ;

        public CurriculumViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_date = (TextView) itemView.findViewById(R.id.tv_date);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_word = (TextView) itemView.findViewById(R.id.tv_word);
            btn_state = (Button) itemView.findViewById(R.id.btn_state);
            /**
             *  每一行的点击事件
             *
             */
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClicklistener.OnItemClick(v, getLayoutPosition()-2);
                }
            });
        }
    }
    /**
     * 单行点击事件接口
     */
    public interface OnItemClicklistener {
        void OnItemClick(View view, int position);
    }

    /**
     * 单行点击事件(外部调用)
     */
    private OnItemClicklistener onItemClicklistener;

    public void setOnItemClicklistener(OnItemClicklistener onItemClicklistener) {
        this.onItemClicklistener = onItemClicklistener;
    }

}
